package 递归;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * @author mubei
 * @date 2021/12/19
 * @description
 */
public class Demo01字符串的排列 {


    public static void main(String[] args) {
        Demo01字符串的排列 demo = new Demo01字符串的排列();
        demo.Permutation("abc");
        System.out.println("ehll");
    }    HashSet<String> set = new HashSet<String>();
    ArrayList<String> res = new ArrayList<>();
    StringBuffer sb = new StringBuffer();
    char chars[] = new char[10];
    boolean isUsed[] = new boolean[10];
    int k = 0;
    public ArrayList<String> Permutation(String str) {
        k = str.length();
        chars = str.toCharArray();
        dfs(0);
        return res;
    }

    void dfs(int n){
        if(n == k){
            if(!set.contains(sb.toString())){
                res.add(sb.toString());
                set.add(sb.toString());
            }

        }
        for (int i = 0; i < k; i++) {
            if(!isUsed[i]){
                isUsed[i] = true;
                sb.append(chars[i]);
                dfs(n+1);
                sb.delete(sb.length()-1,sb.length());
                isUsed[i]=false;
            }
        }

    }

}
